<html>
<head><meta charset="utf-8"><title>things from Stack Overflow · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html">things from Stack Overflow</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="134822833"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/134822833" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#134822833">(Sep 28 2018 at 13:12)</a>:</h4>
<p><a href="https://stackoverflow.com/q/52548161/155423" target="_blank" title="https://stackoverflow.com/q/52548161/155423">Why does modifying a mutable reference's value through a raw pointer not violate Rust's aliasing rules?</a></p>
<p>/cc <span class="user-mention" data-user-id="120791">@RalfJ</span></p>



<a name="134923173"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/134923173" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#134923173">(Sep 30 2018 at 13:31)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> Seems like someone answered :)</p>



<a name="134923218"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/134923218" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#134923218">(Sep 30 2018 at 13:33)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> an important thing about any source is to evaluate it yourself. Voting to affect the score would be useful, but you are knowledgable and recognized (and even mentioned in the answer), so adding a comment that says you agree/disagree or an alternate answer would of course be welcomed</p>



<a name="134923263"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/134923263" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#134923263">(Sep 30 2018 at 13:34)</a>:</h4>
<p>aside: it always amazes me how many people get to a SO question and read the <em>top</em> answer, immediately stopping. They never read the subsequent (often better) answers.</p>



<a name="134923277"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/134923277" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#134923277">(Sep 30 2018 at 13:35)</a>:</h4>
<p>ideally the top answer would be the best one...</p>



<a name="134923325"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/134923325" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#134923325">(Sep 30 2018 at 13:37)</a>:</h4>
<p>I added a comment. If it was my answer I'd also add another example of a piece of code that is NOT okay... what would be the best way to do that?</p>



<a name="134923432"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/134923432" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#134923432">(Sep 30 2018 at 13:40)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> e.g. this code is not okay:</p>
<div class="codehilite"><pre><span></span><span class="kd">let</span><span class="w"> </span><span class="n">x</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="o">&amp;</span><span class="k">mut</span><span class="w"> </span><span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">];</span><span class="w"></span>
<span class="kd">let</span><span class="w"> </span><span class="n">x_ptr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">x</span><span class="p">.</span><span class="n">as_mut_ptr</span><span class="p">();</span><span class="w"></span>

<span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">for</span><span class="w"> </span><span class="n">i</span><span class="w"> </span><span class="k">in</span><span class="w"> </span><span class="mi">0</span><span class="p">..</span><span class="n">x</span><span class="p">.</span><span class="n">len</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="o">*</span><span class="n">x_ptr</span><span class="p">.</span><span class="n">offset</span><span class="p">(</span><span class="n">i</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">isize</span><span class="p">)</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">2</span><span class="p">;</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
<span class="n">assert_eq</span><span class="o">!</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="p">[</span><span class="mi">3</span><span class="p">,</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"> </span><span class="mi">6</span><span class="p">]);</span><span class="w"></span>

<span class="k">unsafe</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="o">*</span><span class="n">x_ptr</span><span class="w"> </span><span class="o">+=</span><span class="w"> </span><span class="mi">1</span><span class="p">;</span><span class="w"> </span><span class="p">}</span><span class="w"> </span><span class="c1">// BÄM! Used no-longer-valid raw ptr</span>
</pre></div>



<a name="134923806"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/134923806" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#134923806">(Sep 30 2018 at 13:52)</a>:</h4>
<p>It's a narrow world view to assume that there can even <em>be</em> a "best".</p>



<a name="134923818"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/134923818" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#134923818">(Sep 30 2018 at 13:53)</a>:</h4>
<p>And there are plenty of arguments around what should even be at the top of the answers (accepted by OP, most votes, most recently created, etc.)</p>



<a name="134923859"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/134923859" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#134923859">(Sep 30 2018 at 13:54)</a>:</h4>
<p>There's a small amount of sorting you can do on the answers, if so inclined</p>



<a name="134923873"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/134923873" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#134923873">(Sep 30 2018 at 13:55)</a>:</h4>
<blockquote>
<p>I'd also add another example of a piece of code that is NOT okay... what would be the best way to do that?</p>
</blockquote>
<p>Again, assuming there's a <em>best</em> <span class="emoji emoji-1f609" title="wink">:wink:</span> </p>
<p>I see a few options. You could provide that code in a playground link and add it and text in a comment (optionally encouraging OP to include it in the answer), you could edit it into the answer, or you could provide an answer yourself</p>



<a name="134923922"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/134923922" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#134923922">(Sep 30 2018 at 13:56)</a>:</h4>
<p>Matthieu M. is a pretty frequent contributor, so I'd expect any communication in comments would be responded to in a short amount of time</p>



<a name="134924037"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/134924037" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#134924037">(Sep 30 2018 at 14:00)</a>:</h4>
<p>Thank you for commenting, <span class="user-mention" data-user-id="120791">@RalfJ</span> !</p>



<a name="134924114"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/134924114" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#134924114">(Sep 30 2018 at 14:03)</a>:</h4>
<p>I dont think I can edit other people's stuff, so I'll try the playground link</p>



<a name="135265082"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/135265082" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#135265082">(Oct 05 2018 at 16:28)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> Uh, I am thoroughly confused by <a href="https://stackoverflow.com/questions/52601731/how-to-create-unsafecellc-void-safely" target="_blank" title="https://stackoverflow.com/questions/52601731/how-to-create-unsafecellc-void-safely">https://stackoverflow.com/questions/52601731/how-to-create-unsafecellc-void-safely</a>. But all the other participants seem to be as well.</p>



<a name="135265092"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/135265092" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#135265092">(Oct 05 2018 at 16:28)</a>:</h4>
<p>I feel like at least 2, probably more, issues are conflated there -- so I am not even sure where to begin answering and/or correcting things^^</p>



<a name="135265176"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/135265176" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#135265176">(Oct 05 2018 at 16:30)</a>:</h4>
<p>But to begin, when you have an opaque FFI <em>pointer</em>, you don't have to worry about <code>UnsafeCell</code> or interior mutability or anything like that. Rust doesnt care about stuff behind a raw pointer.</p>



<a name="135286395"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/135286395" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#135286395">(Oct 05 2018 at 22:39)</a>:</h4>
<p>Ugh. That question is a mess</p>



<a name="135287077"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/135287077" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#135287077">(Oct 05 2018 at 22:55)</a>:</h4>
<p>I think " Rust doesnt care about stuff behind a raw pointer." is the most important aspect; is that already codified in whatever unsafe things exist?</p>



<a name="135303864"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/135303864" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#135303864">(Oct 06 2018 at 08:10)</a>:</h4>
<blockquote>
<p>I think " Rust doesnt care about stuff behind a raw pointer." is the most important aspect; is that already codified in whatever unsafe things exist?</p>
</blockquote>
<p>I am not sure how explicitly it is said -- it is the entire reason raw ptrs even exist, so it may be one of those things that didnt even seem worth saying.^^ but I'd guess it is <em>somewhere</em></p>



<a name="135303875"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/135303875" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#135303875">(Oct 06 2018 at 08:11)</a>:</h4>
<p>e.g. <a href="https://doc.rust-lang.org/nightly/nomicon/ffi.html" target="_blank" title="https://doc.rust-lang.org/nightly/nomicon/ffi.html">https://doc.rust-lang.org/nightly/nomicon/ffi.html</a></p>
<blockquote>
<p>raw pointers fall outside of Rust's safe memory model.</p>
</blockquote>



<a name="135303877"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/135303877" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#135303877">(Oct 06 2018 at 08:11)</a>:</h4>
<p>and there is <a href="https://doc.rust-lang.org/book/2018-edition/ch19-01-unsafe-rust.html#dereferencing-a-raw-pointer" target="_blank" title="https://doc.rust-lang.org/book/2018-edition/ch19-01-unsafe-rust.html#dereferencing-a-raw-pointer">https://doc.rust-lang.org/book/2018-edition/ch19-01-unsafe-rust.html#dereferencing-a-raw-pointer</a></p>



<a name="135312880"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/135312880" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#135312880">(Oct 06 2018 at 13:43)</a>:</h4>
<p>Nice, things I can link to always make an answer better</p>



<a name="135312885"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/135312885" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#135312885">(Oct 06 2018 at 13:43)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> do you think that defining "interior mutability" is something that this WG should be concerned with?</p>



<a name="135344085"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/135344085" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#135344085">(Oct 07 2018 at 08:28)</a>:</h4>
<blockquote>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> do you think that defining "interior mutability" is something that this WG should be concerned with?</p>
</blockquote>
<p>In principle yes, however -- is there an open question there? Interior mutabilitiy is about mutating memory where there also exists a live shared reference non-transitively pointing to the same memory</p>



<a name="135344091"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/135344091" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#135344091">(Oct 07 2018 at 08:29)</a>:</h4>
<p>at least in my mind interior mutability is pretty much settleed</p>



<a name="135357036"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/135357036" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#135357036">(Oct 07 2018 at 16:00)</a>:</h4>
<p>That's fine then. I'll submit a PR to the glossary with that and you can approve it ;-)</p>



<a name="136287232"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136287232" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136287232">(Oct 22 2018 at 19:01)</a>:</h4>
<p><a href="https://stackoverflow.com/q/52911942/155423" target="_blank" title="https://stackoverflow.com/q/52911942/155423">Is transmuting PhantomData markers safe?</a></p>



<a name="136287689"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136287689" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136287689">(Oct 22 2018 at 19:09)</a>:</h4>
<p>we do not currently guarantee any such thing for repr(Rust) structs. in the UCG repo discussion seems to be favorable towards guaranteeing it at least for newtypes (exact definition TBD but might include that type). guaranteeing it for something like { Vec&lt;T&gt;, i32, PhantomData&lt;_&gt; }  seems controversial though personally I think we really should</p>



<a name="136287798"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136287798" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136287798">(Oct 22 2018 at 19:11)</a>:</h4>
<p>Doing my due diligence — should I stick this core question somewhere to ultimately be a part of the Grand Unified Unsafe Theory?</p>



<a name="136288087"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136288087" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136288087">(Oct 22 2018 at 19:16)</a>:</h4>
<p>i don't think there's an actively developed and consulted collection of "stuff we need to settle" any more (there used to be), but with a little massaging this could be an example in an issue comment arguing for ignoring align=1,size=0 fields for repr(Rust) type layout (e.g. here: <a href="https://github.com/rust-rfcs/unsafe-code-guidelines/issues/35" target="_blank" title="https://github.com/rust-rfcs/unsafe-code-guidelines/issues/35">https://github.com/rust-rfcs/unsafe-code-guidelines/issues/35</a>). i don't think the "PhantomData for phantom types that should be possible to change without copying" use case was explicitly spelled out yet</p>



<a name="136288281"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136288281" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136288281">(Oct 22 2018 at 19:20)</a>:</h4>
<p>it'd be good <span class="user-mention" data-user-id="116155">@Jake Goulding</span> to have links to questions that arise in the wild, imo</p>



<a name="136288335"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136288335" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136288335">(Oct 22 2018 at 19:20)</a>:</h4>
<p>I also agree with <span class="user-mention" data-user-id="124289">@rkruppe</span> that I think we should make said guarantee</p>



<a name="136288451"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136288451" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136288451">(Oct 22 2018 at 19:22)</a>:</h4>
<p><span class="user-mention" data-user-id="116009">@nikomatsakis</span> do you agree with adding a comment to the issue that <span class="user-mention" data-user-id="124289">@rkruppe</span> linked me then?</p>



<a name="136288618"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136288618" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nikomatsakis <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136288618">(Oct 22 2018 at 19:24)</a>:</h4>
<p>I do</p>



<a name="136320153"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136320153" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136320153">(Oct 23 2018 at 07:12)</a>:</h4>
<p>I now responded saying they should use <code>repr(C)</code>. seems like the safest choice IMO, and what people should do when playing tricks with transmute and struct layout. you should have good reasons to do what with <code>repr(Rust)</code>.</p>



<a name="136321899"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136321899" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136321899">(Oct 23 2018 at 07:53)</a>:</h4>
<p>The downside of repr(C), particularly for non-newtypes, is that you lose all the nice repr(Rust) optimizations that <em>do</em> make sense for your type. While we could introduce a complex hierarchy of attributes to gradually lower the undefinedness of repr(Rust) layout, I think for very niche or speculative optimizations it's more economical to give nice guarantees about repr(Rust) and instead introduce new reprs for optimizations that go beyond that <em>if and when they are implemented</em></p>



<a name="136322815"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136322815" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136322815">(Oct 23 2018 at 08:13)</a>:</h4>
<p><code>repr(C)</code> kills niches?</p>



<a name="136322869"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136322869" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136322869">(Oct 23 2018 at 08:14)</a>:</h4>
<p>it doesn't kill niches, but it kills reordering the non-align1 fields to minimize padding</p>



<a name="136322964"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136322964" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136322964">(Oct 23 2018 at 08:16)</a>:</h4>
<p>Yeah it doesnt: <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2015&amp;gist=6264c79e56afe2b77c6f51d04c0b6059" target="_blank" title="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2015&amp;gist=6264c79e56afe2b77c6f51d04c0b6059">https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2015&amp;gist=6264c79e56afe2b77c6f51d04c0b6059</a></p>



<a name="136323384"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136323384" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136323384">(Oct 23 2018 at 08:25)</a>:</h4>
<p>for newtypes the reordering doesnt do anything... and I'd rather be cautious and not reorder fields automatically when doing such tricks</p>



<a name="136323435"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136323435" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136323435">(Oct 23 2018 at 08:26)</a>:</h4>
<p>"I cannot always obtain the ideal field order with <code>repr(C)</code>" might be an argument to use <code>repr(Rust)</code>, but IMO one should first see whether <code>repr(C)</code> isn't enough</p>



<a name="136323444"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136323444" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136323444">(Oct 23 2018 at 08:27)</a>:</h4>
<p>if you use PhantomData for actual phantom types and so the type in the phantom data is the only thing you change when transmuting, everything's fine</p>



<a name="136323465"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136323465" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136323465">(Oct 23 2018 at 08:27)</a>:</h4>
<p>maybe it is. so far I dont even see this suggested in the UCG discussion, aside from Gankro asking for something stronger that would imply what you said</p>



<a name="136323472"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136323472" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136323472">(Oct 23 2018 at 08:27)</a>:</h4>
<p>I agree it is reasonable though</p>



<a name="136323527"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136323527" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136323527">(Oct 23 2018 at 08:28)</a>:</h4>
<p>I am kicking myself for not bringing it up before because, duh, obviously</p>



<a name="136323540"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136323540" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136323540">(Oct 23 2018 at 08:29)</a>:</h4>
<p>;)</p>



<a name="136344080"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136344080" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andrew Poelstra <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136344080">(Oct 23 2018 at 15:27)</a>:</h4>
<p><a href="https://queue.acm.org/detail.cfm?id=3212479" target="_blank" title="https://queue.acm.org/detail.cfm?id=3212479">https://queue.acm.org/detail.cfm?id=3212479</a> suggests that C's struct guarantees do actually kill performance (preventing reordering, requiring preservation of padding)</p>



<a name="136344515"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136344515" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136344515">(Oct 23 2018 at 15:32)</a>:</h4>
<p><span class="user-mention" data-user-id="132769">@Andrew Poelstra</span> that's a very interesting article, thanks for sharing!</p>



<a name="136344820"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136344820" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136344820">(Oct 23 2018 at 15:36)</a>:</h4>
<p><span class="user-mention" data-user-id="132769">@Andrew Poelstra</span> I'm not sure what exactly you're responding to but two things to keep in mind in this discussion: 1. nobody except joshtriplett wants repr(Rust) struct layout to be as locked down as repr(C) layout, it's just a question of defaults for certain specialized optimizations, and 2. where hard data exists on the effectiveness of certain layout optimizations, it's usually either limited to minimizing padding (which we already do and want to keep doing) or not distinguishing between speedups caused by optimizations that we already have one hand and more advanced and tricky optimizations on the other hand (because they apply them all in one go to C code) which makes it hard to infer how useful the second kind of optimization would be to add to Rust</p>



<a name="136345047"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136345047" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Andrew Poelstra <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136345047">(Oct 23 2018 at 15:39)</a>:</h4>
<p><span class="user-mention" data-user-id="124289">@rkruppe</span> Ok, cool. I was posted the article mostly because it surprised me with the extent of the optimizations that exist and talk of locking down repr(Rust) makes me nervous that people are trying to make programmers' lives easier for certain niche struct-hacking use cases without considering the extent of the performance loss. But I think I misread the room.</p>



<a name="136345067"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136345067" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136345067">(Oct 23 2018 at 15:39)</a>:</h4>
<p>btw, some of the transformations described/wishlisted in that article specifically, such as AoS&lt;-&gt;SoA, aren't even possible in safe Rust</p>



<a name="136407468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136407468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136407468">(Oct 24 2018 at 13:09)</a>:</h4>
<p><span class="user-mention" data-user-id="116155">@Jake Goulding</span> thanks for editing :)</p>



<a name="136426520"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136426520" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136426520">(Oct 24 2018 at 18:11)</a>:</h4>
<p>no problem; you know how I enjoy it</p>



<a name="136611468"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136611468" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136611468">(Oct 27 2018 at 15:35)</a>:</h4>
<p><a href="https://stackoverflow.com/q/53018458/155423" target="_blank" title="https://stackoverflow.com/q/53018458/155423">Is it UB to cast *mut T to *mut ManuallyDrop&lt;T&gt;?</a></p>



<a name="136611633"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136611633" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136611633">(Oct 27 2018 at 15:40)</a>:</h4>
<p>Isn't pointer-casting always "safe" since only the dereference is potentially unsafe?</p>



<a name="136611642"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136611642" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136611642">(Oct 27 2018 at 15:41)</a>:</h4>
<p>(it can be done in safe code...)</p>



<a name="136612094"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136612094" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136612094">(Oct 27 2018 at 15:55)</a>:</h4>
<p>heh, that's my kind of pedantry <span class="emoji emoji-1f609" title="wink">:wink:</span></p>



<a name="136612103"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136612103" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136612103">(Oct 27 2018 at 15:55)</a>:</h4>
<p>I <em>assume</em> they mean to then use the dereferenced value.</p>



<a name="136612108"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136612108" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136612108">(Oct 27 2018 at 15:55)</a>:</h4>
<p>But more specifically, I think this is perfectly valid -- because <code>ManuallyDrop</code> is <code>#[repr(transparent)]</code></p>



<a name="136612166"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136612166" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136612166">(Oct 27 2018 at 15:57)</a>:</h4>
<p>Though I might say that <code>let foo: ManuallyDrop&lt;T&gt; = ptr::read(v: *mut T);</code> is how I'd do it</p>



<a name="136612174"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136612174" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136612174">(Oct 27 2018 at 15:57)</a>:</h4>
<p>Though I guess you can't do that directly because read requires *const T -&gt; T</p>



<a name="136612175"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136612175" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136612175">(Oct 27 2018 at 15:57)</a>:</h4>
<p>but yeah this seems fine to me</p>



<a name="136612234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136612234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136612234">(Oct 27 2018 at 15:59)</a>:</h4>
<p><code>repr(transparent)</code> was my rationale as well.</p>



<a name="136612299"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136612299" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136612299">(Oct 27 2018 at 16:00)</a>:</h4>
<p>but then there's the question of the <em>value</em> of a pointer to a <code>ManuallyDrop</code> in the first place</p>



<a name="136612334"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136612334" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136612334">(Oct 27 2018 at 16:00)</a>:</h4>
<p>If you don't want to drop it, don't read it — convert to a reference instead.</p>



<a name="136612462"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136612462" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136612462">(Oct 27 2018 at 16:04)</a>:</h4>
<p>That's true -- or <code>ptr::write</code> to the value</p>



<a name="136612470"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136612470" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136612470">(Oct 27 2018 at 16:04)</a>:</h4>
<p>I guess if you want to write to it "normally" though <code>ManuallyDrop</code> makes <code>&amp;mut ManuallyDrop&lt;T&gt;</code> a "safe" conversion</p>



<a name="136612476"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/136612476" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> simulacrum <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#136612476">(Oct 27 2018 at 16:04)</a>:</h4>
<p>(given that it's dereferenceable)</p>



<a name="154142092"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/154142092" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#154142092">(Jan 01 2019 at 20:33)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> your name was mentioned in the comments, so on the off-chance you might be interested...</p>
<p><a href="https://stackoverflow.com/q/53990135/155423" target="_blank" title="https://stackoverflow.com/q/53990135/155423">Is using an immutable borrow after an aliasing mutable borrow is created but before it is used actually dangerous?</a></p>



<a name="154179462"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things%20from%20Stack%20Overflow/near/154179462" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/things.20from.20Stack.20Overflow.html#154179462">(Jan 02 2019 at 15:03)</a>:</h4>
<p>thanks! Note that in your example, <code>f</code> should also get <code>restrict</code>. it applies to all mutable references as well as shared refs without immediate (non-transitive) interior mutability.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>